﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace 监控程序
{
    class ServiceInstall
    {
        #region dllimport
        [DllImport("advapi32.dll", SetLastError = true)]
        protected static extern IntPtr OpenSCManager(string lpmachinename, string lpscdb, int scparameter);
        [DllImport("advapi32.dll", SetLastError = true)]
        protected static extern IntPtr CreateService(IntPtr sc_handle, string lpsvcname, string lpdisplayname,
            int dwdesiredaccess, int dwservicetype, int dwstarttype, int dwerrorcontrol, string lppathname,
            string lploadordergroup, int lpdwtagid, string lpdependencies, string lpservicestartname, string lppassword);
        [DllImport("advapi32.dll", SetLastError = true)]
        protected static extern void CloseServiceHandle(IntPtr schandle);
        [DllImport("advapi32.dll", SetLastError = true)]
        protected static extern int StartService(IntPtr svhandle, int dwnumserviceargs, string lpserviceargvectors);
        [DllImport("advapi32.dll", SetLastError = true)]
        protected static extern IntPtr OpenService(IntPtr schandle, string lpsvcname, int dwnumserviceargs);
        [DllImport("advapi32.dll", SetLastError = true)]
        protected static extern int DeleteService(IntPtr svhandle);
        [DllImport("kernel32.dll", SetLastError = true)]
        protected static extern int GetLastError();
        #endregion dllimport

        public static bool InstallService(string svcpath, string svcname, string svcdispname)
        {
            #region constants declaration.
            int sc_manager_create_service = 0x0002;
            int service_win32_own_process = 0x00000010;
            //int service_interactive_process = 0x00000100;
            int service_error_normal = 0x00000001;
            int standard_rights_required = 0xf0000;
            int service_query_config = 0x0001;
            int service_change_config = 0x0002;
            int service_query_status = 0x0004;
            int service_enumerate_dependents = 0x0008;
            int service_start = 0x0010;
            int service_stop = 0x0020;
            int service_pause_continue = 0x0040;
            int service_interrogate = 0x0080;
            int service_user_defined_control = 0x0100;
            int service_all_access = (standard_rights_required |service_query_config |
                service_change_config |service_query_status |service_enumerate_dependents |
                service_start |service_stop |service_pause_continue | service_interrogate |service_user_defined_control);
            int service_auto_start = 0x00000002;
            #endregion constants declaration.
            try
            {
                IntPtr sc_handle = OpenSCManager(null, null, sc_manager_create_service);
                if (sc_handle.ToInt32() != 0)
                {
                    IntPtr sv_handle = CreateService(sc_handle, svcname, svcdispname, service_all_access, service_win32_own_process, service_auto_start, service_error_normal, svcpath, null, 0, null, null, null);
                    if (sv_handle.ToInt32() == 0)
                    {
                        CloseServiceHandle(sc_handle);
                        return false;
                    }
                    else
                    {
                        ////试尝启动服务
                        //int i = StartService(sv_handle, 0, null);
                        //if (i == 0)
                        //{
                        //    return false;
                        //}

                        CloseServiceHandle(sv_handle);
                        CloseServiceHandle(sc_handle);
                        return true;
                    }
                }
                else

                    return false;
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        public static bool UnInstallService(string svcname)
        {
            int generic_write = 0x40000000;
            IntPtr sc_hndl = OpenSCManager(null, null, generic_write);
            if (sc_hndl.ToInt32() != 0)
            {
                int delete = 0x10000;
                IntPtr svc_hndl = OpenService(sc_hndl, svcname, delete);
                if (svc_hndl.ToInt32() != 0)
                {
                    int i = DeleteService(svc_hndl);
                    CloseServiceHandle(svc_hndl);
                    if (i != 0)
                    {
                        CloseServiceHandle(sc_hndl);
                        return true;
                    }
                    else
                    {
                        CloseServiceHandle(sc_hndl);
                        return false;
                    }
                }
                else
                    return false;
            }
            else
                return false;
        }
    }
}
